<?php
// +-------------------------------
// | 中润报表系统
// +-------------------------------
// | author: zw
// +-------------------------------

namespace app\adminapi\controller\report;

use app\adminapi\controller\BaseAdminController;
use think\facade\Db;
use think\facade\Log;

/**
 * 报表提交控制器
 * Class SubmitController
 * @package app\adminapi\controller\report
 */
class SubmitController extends BaseAdminController
{
    public $notNeedLogin = ['getSqlServer','crontab','crontab2','crontab3','crontab4'];
    /**
     * @notes  原料煤管理情况获取
     * @return \think\response\Json
     * @author zw
     * @date 2023/1/17 23:59
     */
    public function getYlmglqk()
    {
        // 接受所有参数
        $data = input('param.');
        $date = !empty($data['date'])?$data['date']:date('Y-m-d',strtotime('-1 day'));
        
        $where = [
            "date"=>$date,
        ];
        if(!empty($data['ishistory'])){
            $where['status'] = 2;
        }
        $is = Db::name('ylmglqk')->where($where)->select()->toArray();
        if(count($is)==0&&empty($data['ishistory'])&&empty($data['isup'])){
            // 获取前一天数据
            $where['date'] = date("Y-m-d",(strtotime($date)-86400));
            $is = Db::name('ylmglqk')->where($where)->select()->toArray();
            foreach ($is as &$value) {
                $value['date'] = $date;
                $value['update_time'] = date('Y-m-d H:i:s');
                unset($value['id']);
                unset($value['aid']);
                unset($value['aname']);
                unset($value['status']);
                unset($value['cid']);
                unset($value['cname']);
                unset($value['check_time']);
            }
            Db::name('ylmglqk')->insertAll($is);
        }
        $cmcl = Db::name('pmzbjjtzlqk')->field('sum(cl) as cl')->where('date',$date)->group('date')->find();
        return $this->data(['data'=>$is,'cmclnumber'=>!empty($cmcl['cl'])?$cmcl['cl']:0]);
    }
    
    /**
     * @notes  原料煤管理情况保存
     * @return \think\response\Json
     * @author zw
     * @date 2023/1/17 21:59
     */
    public function ylmglqkSave()
    {
        // 接受所有参数
        $data = input('param.');
        // 拿到循环插入的数据
        $inputs = $data['inputs'];
        $arr = [];
        $date = !empty($data['date'])?$data['date']:date('Y-m-d',strtotime('-1 day'));
        // 处理数据  匹配字段 并且添加当日时间
        foreach ($inputs as $key => $value) {
            $arr[$key]['aid'] = $data['aid'];
            $arr[$key]['aname'] = $data['aname'];
            foreach ($value as $k => $v) {
                $kk = rtrim($k,$key+1);
                $arr[$key][$kk] = $v;
            }
            $arr[$key]['jnmcl'] = $data['jnmcl'];
            $arr[$key]['content'] = $data['content'];
            $arr[$key]['date'] = $date;
            $arr[$key]['update_time'] = date('Y-m-d H:i:s');
        }
        $is = Db::name('ylmglqk')->where('date',$date)->select();
        // 如果已经存在当日数据  就更新  否则就新增
        if(count($is)>0){
            $urole = Db::name('admin_role')->where('admin_id',$data['aid'])->find();
            if($data['aid']!=1){
                if($is[0]['status']==1&&!in_array($urole['role_id'],[3,4])){
                    return $this->fail('数据已经上报，不能在进行修改！如有问题请联系生产调度');
                }
                if($is[0]['status']==2&&!in_array($urole['role_id'],[3,4])){
                    return $this->fail('数据已经审核，不能在进行修改！如有问题请联系生产调度');
                }
            }
            foreach ($is as $key => $value) {
                $res = Db::name('ylmglqk')->where('id',$value['id'])->update($arr[$key]);
                if(!$res){
                    return $this->fail('保存失败!');
                }
            }
        }else{
            $res = Db::name('ylmglqk')->insertAll($arr);
        }

        if($res){
            return $this->success('保存成功', [], 1, 1);
        }
        return $this->fail('保存失败');
    }

    /**
     * @notes  原料煤管理情况上报
     * @return \think\response\Json
     * @author zw
     * @date 2023/1/17 21:59
     */
    public function ylmglqkSubmit()
    {
        $data = input('param.');
        $date = !empty($data['date'])?$data['date']:date('Y-m-d',strtotime('-1 day'));
        if(in_array($data['label'],['生产调度','中润管理员','系统管理员'])){
            $isarr = Db::name('ylmglqk')->where('date',$date)->select();
            if(!empty($isarr)){
                $is['status'] = 0;
            }
        }else{
            $is = Db::name('ylmglqk')->where('date',$date)->find();
        }
        if(!empty($is)){
            // 未提交或审核驳回，就可以上报
            if($is['status']==0||$is['status']==3){
                Db::name('ylmglqk')->where('date',$date)->update([
                    'status'=>1
                ]);
                return $this->success('上报成功', [], 1, 1);
            }elseif($is['status']==1){
                return $this->fail('数据已上报，不能重复上报');
            }elseif($is['status']==2){
                return $this->fail('数据已审核，无法上报');
            }
        }else{
            return $this->fail('请先保存数据，保存后才可上报');
        }
    }
    
    /**
     * @notes  配煤指标及焦炭质量情况 获取
     * @return \think\response\Json
     * @author zw
     * @date 2023/1/17 23:59
     */
    public function getPmzbjjtzlqk()
    {
        // 接受所有参数
        $data = input('param.');
        $date = !empty($data['date'])?$data['date']:date('Y-m-d',strtotime('-1 day'));
        $is = Db::name('pmzbjjtzlqk')->where('date',$date)->select()->toArray();
        if(count($is)==0&&empty($data['isup'])){
            // 获取前一天数据
            $yestoday = date("Y-m-d",(strtotime($date)-86400));
            $is = Db::name('pmzbjjtzlqk')->where('date',$yestoday)->select()->toArray();
            foreach ($is as &$value) {
                $value['date'] = $date;
                $value['update_time'] = date('Y-m-d H:i:s');
                unset($value['id']);
                unset($value['aid']);
                unset($value['aname']);
                unset($value['status']);
                unset($value['cid']);
                unset($value['cname']);
                unset($value['check_time']);
            }
            Db::name('pmzbjjtzlqk')->insertAll($is);
        }
        return $this->data($is);
    }

    /**
     * @notes  配煤指标及焦炭质量情况保存
     * @return \think\response\Json
     * @author zw
     * @date 2023/1/27 21:59
     */
    public function pmzbjjtzlqkSave()
    {
        // 接受所有参数
        $data = input('param.');
        // 拿到循环插入的数据
        $inputs = $data['inputs'];
        $arr = [];
        $date = !empty($data['date'])?$data['date']:date('Y-m-d',strtotime('-1 day'));
        
        // 处理数据  匹配字段 并且添加当日时间
        foreach ($inputs as $key => $value) {
            $arr[$key]['aid'] = $data['aid'];
            $arr[$key]['aname'] = $data['aname'];
            foreach ($value as $k => $v) {
                if(strpos($k,'_')){
                    $kk = rtrim($k,'_'.($key+1));
                }else{
                    $kk = $k;
                }
                $arr[$key][$kk] = $v;
            }
            $arr[$key]['content'] = $data['content'];
            $arr[$key]['jdkhmq'] = $data['jdkhmq'];
            $arr[$key]['mtcl1'] = $data['mtcl1'];
            $arr[$key]['mtcl2'] = $data['mtcl2'];
            $arr[$key]['jiachun'] = $data['jiachun'];
            $arr[$key]['jinghua'] = $data['jinghua'];
            $arr[$key]['qumei'] = $data['qumei'];
            $arr[$key]['rlmzb'] = json_encode($data['rlmzb']);
            $arr[$key]['date'] = $date;
            $arr[$key]['update_time'] = date('Y-m-d H:i:s');
        }
        $is = Db::name('pmzbjjtzlqk')->where('date',$date)->select()->toArray();
        
        // 如果已经存在当日数据  就更新  否则就新增
        if(count($is)>0){
            if($data['aid']!=1){
                $urole = Db::name('admin_role')->where('admin_id',$data['aid'])->find();
                if($is[0]['isbm']==1&&$is[0]['iszj']==1){
                    if($is[0]['status']==1&&!in_array($urole['role_id'],[3,4])){
                        return $this->fail('数据已经上报，不能在进行修改！如有问题请联系生产调度');
                    }
                }
                if($is[0]['status']==2&&!in_array($urole['role_id'],[3,4])){
                    return $this->fail('数据已经审核，不能在进行修改！如有问题请联系生产调度');
                }
            }
            
            foreach ($is as $key => $value) {
                $res = Db::name('pmzbjjtzlqk')->where('id',$value['id'])->update($arr[$key]);
                if(!$res){
                    return $this->fail('保存失败!');
                }
            }
        }else{
            $res = Db::name('pmzbjjtzlqk')->insertAll($arr);
        }

        if($res){
            return $this->success('保存成功', [], 1, 1);
        }
        return $this->fail('保存失败');
    }

    /**
     * @notes  配煤指标及焦炭质量情况上报
     * @return \think\response\Json
     * @author zw
     * @date 2023/1/17 21:59
     */
    public function pmzbjjtzlqkSubmit()
    {
        $data = input('param.');
        $date = !empty($data['date'])?$data['date']:date('Y-m-d',strtotime('-1 day'));
        $up = [
            "status"=>1
        ];
        if(in_array($data['label'],['生产调度','中润管理员','系统管理员'])){
            $isarr = Db::name('pmzbjjtzlqk')->where('date',$date)->select();
            if(!empty($isarr)){
                $is['status'] = 0;
            }
        }else{
            if($data['label']=='备煤分厂'){//备煤分厂
                $up['isbm'] = 1;
            }elseif($data['label']=='质检中心'){//质检中心
                $up['iszj'] = 1;
            }else{//管理员
                $up['isbm'] = 1;
                $up['iszj'] = 1;
            }
            $is = Db::name('pmzbjjtzlqk')->where('date',$date)->find();
        }
        if(!empty($is)){
            // 未提交或审核驳回，就可以上报
            if($is['status']==0||$is['status']==3||$is['isbm']==0||$is['iszj']==0){
                Db::name('pmzbjjtzlqk')->where('date',$date)->update($up);
                return $this->success('上报成功', [], 1, 1);
            }elseif($is['status']==1){
                return $this->fail('数据已上报，不能重复上报');
            }elseif($is['status']==2){
                return $this->fail('数据已审核，无法上报');
            }
        }else{
            return $this->fail('请先保存数据，保存后才可上报');
        }
    }

    /**
     * @notes  产品销售及化工类原料进出厂情况 获取
     * @return \think\response\Json
     * @author zw
     * @date 2023/1/17 23:59
     */
    public function getCpxsjhglyljcqk()
    {
        // 接受所有参数
        $data = input('param.');
        $date = !empty($data['date'])?$data['date']:date('Y-m-d',strtotime('-1 day'));
        $is = Db::name('cpxsjhglyljcqk')->where('date',$date)->select()->toArray();
        if(count($is)==0&&empty($data['isup'])){
            // 获取前一天数据
            $yestoday = date("Y-m-d",(strtotime($date)-86400));
            $is = Db::name('cpxsjhglyljcqk')->where('date',$yestoday)->select()->toArray();
            foreach ($is as &$value) {
                $value['date'] = $date;
                $value['update_time'] = date('Y-m-d H:i:s');
                unset($value['id']);
                unset($value['aid']);
                unset($value['aname']);
                unset($value['status']);
                unset($value['cid']);
                unset($value['cname']);
                unset($value['check_time']);
            }
            Db::name('cpxsjhglyljcqk')->insertAll($is);
        }

        $sqlserver = Db::name('sqlserver')->where('date',$date)->select()->toArray();
        $arr = [];
        foreach ($sqlserver as $key => $value) {
            if($value['type']=='进厂'){
                $arr[0][] = $value;
            }else{
                $arr[1][] = $value;
            }
        }

        return $this->data(['is'=>$is,'sqlserver'=>$arr]);
    }

    // 0点物流系统采集化工类供销数据
    public function getSqlServer(){
		$date = date('Y-m-d',strtotime('-1 day'));
        try {
            $res1 = Db::connect('sqlsrv')->query("select * from dbo.Trade where DateDiff(dd,seconddatetime,getdate())=1  and sentflag='进厂' and product in ('硫酸','液体氢氧化钠','洗油','氨水','浓氨水') and datastatus<>9 and net>500");

            $res2 = Db::connect('sqlsrv')->query("select * from dbo.Trade where DateDiff(dd,seconddatetime,getdate())=1 and sentflag='出厂' and product in ('硫酸铵','硫磺','甲醇','焦油') and datastatus<>9 and net>500");

            // '纯苯','甲苯','二甲苯','重苯','环戊烷','C9'   这几种要走E磅 fststationname='E' and scndstationname='E'
            $res3 = Db::connect('sqlsrv')->query("select * from dbo.Trade where DateDiff(dd,seconddatetime,getdate())=1 and fststationname='E' and scndstationname='E' and sentflag='出厂' and product in ('纯苯','甲苯','二甲苯','重苯','环戊烷','C9') and datastatus<>9 and net>500");

            $res = array_merge($res1,$res2,$res3);
			$arr = [];
			foreach($res as $key => $value){
				$arr[$value['sentflag']][] = $value; 
			}
            // 整体月累计时间：上月29日0:00至当月28日24:00   如果没有29日 就从本月1日开始

            // 也就是说  如果是29号之前的 就统计上个月29日到本月当前日期   如果是29号之后的  就统计29号 到 当前的
            $lastDateNum=date('d',strtotime('-1 day'));
            if($lastDateNum<29){
                $y = date('Y');
                $m = date('m',strtotime('-1 month'));
				if($m==2){
					$start = date('Y-m-01 00:00:00.000');
				}else{
					$start = $y.'-'.$m.'-29 00:00:00.000';
				}
            }else{
                $start = date('Y-m-29 00:00:00.000');
            }
            // 当前日期
            $end = date('Y-m-d 00:00:00.000');
            // （月累计进料）查询当月的数据 并且 求和   进厂，出厂各产品的和
            $jcsum1 = Db::connect('sqlsrv')->query("SELECT sentflag,product,SUM(net) as net FROM [dbo].[Trade] where seconddatetime>='$start' and seconddatetime<'$end' and (sentflag='进厂' or sentflag='出厂') and product in ('硫酸','液体氢氧化钠','洗油','氨水','浓氨水','硫酸铵','硫磺','甲醇','焦油') and datastatus<>9 and net>500 GROUP BY product,sentflag");

            $jcsum2 = Db::connect('sqlsrv')->query("SELECT sentflag,product,SUM(net) as net FROM [dbo].[Trade] where seconddatetime>='$start' and seconddatetime<'$end' and fststationname='E' and scndstationname='E' and (sentflag='进厂' or sentflag='出厂') and product in ('纯苯','甲苯','二甲苯','重苯','环戊烷','C9') and datastatus<>9 and net>500 GROUP BY product,sentflag");

            $jcsum = array_merge($jcsum1,$jcsum2);
            $jcarr = [];
            foreach ($jcsum as $key => $value) {
                if($value['product']=='氨水'||$value['product']=='浓氨水'){
                    if(empty($jcarr[$value['sentflag']]['氨水'])){
                        $jcarr[$value['sentflag']]['氨水']=0;
                    }
                    $jcarr[$value['sentflag']]['氨水'] += $value['net'];
                }else{
                    $jcarr[$value['sentflag']][$value['product']] = $value['net'];
                }
            }

            $newarr = [];
			$cheshu = [];
            foreach ($arr as $key => $value) {
                foreach ($value as $k => $v) {
                    if($v['product']=='氨水'||$v['product']=='浓氨水'){
                        $cheshu[$key]['氨水'][] = 1;
                        $newarr[$key]['氨水']['name'] = '氨水';//物料名称
                        @$newarr[$key]['氨水']['net'] += !empty($v['net'])?($v['net']):0;//净重
                        $newarr[$key]['氨水']['cheshu'] = count($cheshu[$key]['氨水']);//进 出料车数
                        $newarr[$key]['氨水']['yueleiji'] = !empty($jcarr[$key]['氨水'])?($jcarr[$key]['氨水']):0;//月累计进 出料
                    }else{
                        $cheshu[$key][$v['product']][] = 1;
                        $newarr[$key][$v['product']]['name'] = $v['product'];//物料名称
                        @$newarr[$key][$v['product']]['net'] += !empty($v['net'])?($v['net']):0;//净重
                        $newarr[$key][$v['product']]['cheshu'] = count($cheshu[$key][$v['product']]);//进 出料车数
                        $newarr[$key][$v['product']]['yueleiji'] = !empty($jcarr[$key][$v['product']])?($jcarr[$key][$v['product']]):0;//月累计进 出料
                    }
                }
            }

            foreach ($jcarr as $key => $value) {
                foreach ($value as $k => $v) {
                    if(empty($newarr[$key][$k])){
                        $newarr[$key][$k]['name'] = $k;//物料名称
                        $newarr[$key][$k]['net'] = 0;//净重
                        $newarr[$key][$k]['cheshu'] = 0;//进 出料车数
                        $newarr[$key][$k]['yueleiji'] = $v;//月累计进 出料
                    }
                }
            }
			
            $data = [];
            foreach ($newarr as $key => $value) {
                foreach ($value as $k => $v) {
                    $data[$v['name']] = [
                        'name'=>$v['name'],
                        'net'=>$v['net']/1000,
                        'cheshu'=>$v['cheshu'],
                        'yueleiji'=>$v['yueleiji']/1000,
                        'kucun'=>0,
                        'type'=>$key,
                        'date'=>$date,
                        'update_time'=>date('Y-m-d H:i:s')
                    ];
                }
            }
            $jname = $cname = [];
            foreach ($data as $key => $value) {
                if($value['type']=='进厂'){
                    $jname[] = $value['name'];
                }else{
                    $cname[] = $value['name'];
                }
            }
            $newdata = [];
            $jinchangname = ['硫酸','液体氢氧化钠','洗油','氨水'];
            foreach ($jinchangname as $key => $value) {
                if(empty($data[$value])){
                    $info = Db::name('sqlserver')->where('name',$value)->order('id','desc')->find();
                    if($lastDateNum==29){
                        $info['yueleiji'] = 0;
                    }
                    $newdata[] = [
                        'name'=>$value,
                        'net'=>0,
                        'cheshu'=>0,
                        'yueleiji'=>!empty($info['yueleiji'])?$info['yueleiji']:0,
                        'kucun'=>0,
                        'type'=>"进厂",
                        'date'=>$date,
                        'update_time'=>date('Y-m-d H:i:s')
                    ];
                }else{
                    $newdata[] = $data[$value];
                }
            }
            $chuchangname = ['硫酸铵','硫磺','甲醇','纯苯','甲苯','二甲苯','重苯','环戊烷','C9','焦油'];
            foreach ($chuchangname as $key => $value) {
                if(empty($data[$value])){
                    $info = Db::name('sqlserver')->where('name',$value)->order('id','desc')->find();
                    if($lastDateNum==29){
                        $info['yueleiji'] = 0;
                    }
                    $newdata[] = [
                        'name'=>$value,
                        'net'=>0,
                        'cheshu'=>0,
                        'yueleiji'=>!empty($info['yueleiji'])?$info['yueleiji']:0,
                        'kucun'=>0,
                        'type'=>"出厂",
                        'date'=>$date,
                        'update_time'=>date('Y-m-d H:i:s')
                    ];
                }else{
                    $newdata[] = $data[$value];
                }
            }
            foreach ($newdata as $key => $v) {
                $kucun = 0;
                // if($v['name']=='硫磺'){
                //     $newdata[$key] = [
                //         'name'=>$v['name'],
                //         'cheshu'=>null,
                //         'net'=>null,
                //         'yueleiji'=>null,
                //         'kucun'=>$v['kucun'],
                //         'type'=>$v['type'],
                //         'date'=>$v['date'],
                //         'update_time'=>$v['update_time']
                //     ];
                // }
                if($v['name']=='甲醇'){
                    $jiachun = Db::name('jcscqk')->where('date',$date)->find();
                    if(!empty($jiachun)) $kucun = $jiachun['agkc']+$jiachun['bgkc'];
                }
                if($v['name']=='纯苯'){
                    $jingben = Db::name('jbscqk')->where('date',$date)->find();
                    if(!empty($jingben)) $kucun = $jingben['cbcl5']+$jingben['cbcl6'];
                }
                if($v['name']=='甲苯'){
                    $jingben = Db::name('jbscqk')->where('date',$date)->find();
                    if(!empty($jingben)) $kucun = ($jingben['jb7']+$jingben['jb8'])*82;
                }
                if($v['name']=='二甲苯'){
                    $jingben = Db::name('jbscqk')->where('date',$date)->find();
                    if(!empty($jingben)) $kucun = $jingben['ejb9']*46;
                }
                if($v['name']=='重苯'){
                    $jingben = Db::name('jbscqk')->where('date',$date)->find();
                    if(!empty($jingben)) $kucun = $jingben['zb13']*46;
                }
                if($v['name']=='环戊烷'){
                    $jingben = Db::name('jbscqk')->where('date',$date)->find();
                    if(!empty($jingben)) $kucun = $jingben['ffj12']*46;
                }
                if($v['name']=='C9'){
                    $jingben = Db::name('jbscqk')->where('date',$date)->find();
                    if(!empty($jingben)) $kucun = $jingben['c8']*46;
                }
                $newdata[$key]['kucun'] = $kucun;
            }
            $is = Db::name('sqlserver')->where('date',$date)->find();
            if(empty($is)){
                foreach($newdata as $value){
					Db::name('sqlserver')->insert($value);
				}
            }
            echo 'success';
        } catch (\Exception $e) {
            Log::record($date.'0点物流系统采集化工类供销数据 失败，失败原因：'.$e->getMessage(),'error');
        }
    }

    /**
     * @notes  产品销售及化工类原料进出厂情况保存
     * @return \think\response\Json
     * @author zw
     * @date 2023/1/27 21:59
     */
    public function cpxsjhglyljcqkSave()
    {
        // 接受所有参数
        $data = input('param.');
        // 拿到循环插入的数据
        $inputs = $data['inputs'];
        $arr = [];
        $date = !empty($data['date'])?$data['date']:date('Y-m-d',strtotime('-1 day'));
        // 处理数据  匹配字段 并且添加当日时间
        foreach ($inputs as $key => $value) {
            $arr[$key]['aid'] = $data['aid'];
            $arr[$key]['aname'] = $data['aname'];
            foreach ($value as $k => $v) {
                $kk = rtrim($k,$key+1);
                $arr[$key][$kk] = $v;
            }
            $arr[$key]['no'] = $key+1;
            $arr[$key]['gjrtotal'] = $data['gjrtotal'];
            $arr[$key]['gjytotal'] = $data['gjytotal'];
            $arr[$key]['jdrtotal'] = $data['jdrtotal'];
            $arr[$key]['jdytotal'] = $data['jdytotal'];
            $arr[$key]['sjfrtotal'] = $data['sjfrtotal'];
            $arr[$key]['sjfytotal'] = $data['sjfytotal'];
            $arr[$key]['gjfrtotal'] = $data['gjfrtotal'];
            $arr[$key]['gjfytotal'] = $data['gjfytotal'];
            $arr[$key]['jmrtotal'] = $data['jmrtotal'];
            $arr[$key]['jmytotal'] = $data['jmytotal'];
            $arr[$key]['jiao65'] = $data['jiao65'];
            $arr[$key]['jiao63'] = $data['jiao63'];
            $arr[$key]['shijiao'] = $data['shijiao'];
            $arr[$key]['kucunzongji'] = $data['kucunzongji'];
            $arr[$key]['content'] = $data['content'];
            $arr[$key]['date'] = $date;
            $arr[$key]['update_time'] = date('Y-m-d H:i:s');
        }
        $is = Db::name('cpxsjhglyljcqk')->where('date',$date)->select();
        // 如果已经存在当日数据  就更新  否则就新增
        if(count($is)>0){
            $urole = Db::name('admin_role')->where('admin_id',$data['aid'])->find();
            if($data['aid']!=1){
                if($is[0]['status']==1&&!in_array($urole['role_id'],[3,4])){
                    return $this->fail('数据已经上报，不能在进行修改！如有问题请联系生产调度');
                }
                if($is[0]['status']==2&&!in_array($urole['role_id'],[3,4])){
                    return $this->fail('数据已经审核，不能在进行修改！如有问题请联系生产调度');
                }
            }

            // 更新进厂库存
            if(!empty($data['jinchang'])){
                foreach ($data['jinchang'] as $key => $value) {
                    if(!empty($value['id'])){
                        $res = Db::name('sqlserver')->where('id',$value['id'])->update($value);
                    }
                }
            }
            
            // 更新出厂库存
            if(!empty($data['chuchang'])){
                foreach ($data['chuchang'] as $key => $value) {
                    if(!empty($value['id'])){
                        $res = Db::name('sqlserver')->where('id',$value['id'])->update($value);
                    }
                }
            }

            foreach ($is as $key => $value) {
                $res = Db::name('cpxsjhglyljcqk')->where('id',$value['id'])->update($arr[$key]);
                if(!$res){
                    return $this->fail('保存失败!');
                }
            }
        }else{
            $res = Db::name('cpxsjhglyljcqk')->insertAll($arr);
        }

        if($res){
            return $this->success('保存成功', [], 1, 1);
        }
        return $this->fail('保存失败');
    }

    /**
     * @notes  产品销售及化工类原料进出厂情况上报
     * @return \think\response\Json
     * @author zw
     * @date 2023/1/17 21:59
     */
    public function cpxsjhglyljcqkSubmit()
    {
        $data = input('param.');
        $date = !empty($data['date'])?$data['date']:date('Y-m-d',strtotime('-1 day'));
        if(in_array($data['label'],['生产调度','中润管理员','系统管理员'])){
            $isarr = Db::name('cpxsjhglyljcqk')->where('date',$date)->select();
            if(!empty($isarr)){
                $is['status'] = 0;
            }
        }else{
            $is = Db::name('cpxsjhglyljcqk')->where('date',$date)->find();
        }
        if(!empty($is)){
            // 未提交或审核驳回，就可以上报
            if($is['status']==0||$is['status']==3){
                Db::name('cpxsjhglyljcqk')->where('date',$date)->update([
                    'status'=>1
                ]);
                Db::name('sqlserver')->where('date',$date)->update([
                    'status'=>1
                ]);
                return $this->success('上报成功', [], 1, 1);
            }elseif($is['status']==1){
                return $this->fail('数据已上报，不能重复上报');
            }elseif($is['status']==2){
                return $this->fail('数据已审核，无法上报');
            }
        }else{
            return $this->fail('请先保存数据，保存后才可上报');
        }
    }


    /**
     * @notes  焦炉生产及工艺控制情况 获取
     * @return \think\response\Json
     * @author zw
     * @date 2023/1/28 23:59
     */
    public function getJlscjgykzqk()
    {
        // 接受所有参数
        $data = input('param.');
        $date = !empty($data['date'])?$data['date']:date('Y-m-d',strtotime('-1 day'));
        $is = Db::name('jlscjgykzqk')->where('date',$date)->select()->toArray();
        if(count($is)==0&&empty($data['isup'])){
            // 获取前一天数据
            $yestoday = date("Y-m-d",(strtotime($date)-86400));
            $is = Db::name('jlscjgykzqk')->where('date',$yestoday)->select()->toArray();
            foreach ($is as &$value) {
                $value['date'] = $date;
                $value['update_time'] = date('Y-m-d H:i:s');
                unset($value['id']);
                unset($value['aid']);
                unset($value['aname']);
                unset($value['status']);
                unset($value['cid']);
                unset($value['cname']);
                unset($value['check_time']);
            }
            Db::name('jlscjgykzqk')->insertAll($is);
        }
        return $this->data($is);
    }

    /**
     * @notes  焦炉生产及工艺控制情况保存
     * @return \think\response\Json
     * @author zw
     * @date 2023/1/28 21:59
     */
    public function jlscjgykzqkSave()
    {
        // 接受所有参数
        $data = input('param.');
        // 拿到循环插入的数据
        $inputs = $data['inputs'];
        $arr = [];
        $date = !empty($data['date'])?$data['date']:date('Y-m-d',strtotime('-1 day'));
        // 处理数据  匹配字段 并且添加当日时间
        foreach ($inputs as $key => $value) {
            $arr[$key]['aid'] = $data['aid'];
            $arr[$key]['aname'] = $data['aname'];
            foreach ($value as $k => $v) {
                $kk = rtrim($k,$key+1);
                $arr[$key][$kk] = $v;
            }
            $arr[$key]['content'] = $data['content'];
            $arr[$key]['date'] = $date;
            $arr[$key]['update_time'] = date('Y-m-d H:i:s');
        }
        $is = Db::name('jlscjgykzqk')->where('date',$date)->select();
        // 如果已经存在当日数据  就更新  否则就新增
        if(count($is)>0){
            $urole = Db::name('admin_role')->where('admin_id',$data['aid'])->find();
            if($data['aid']!=1){
                if($is[0]['status']==1&&!in_array($urole['role_id'],[3,4])){
                    return $this->fail('数据已经上报，不能在进行修改！如有问题请联系生产调度');
                }
                if($is[0]['status']==2&&!in_array($urole['role_id'],[3,4])){
                    return $this->fail('数据已经审核，不能在进行修改！如有问题请联系生产调度');
                }
            }
            foreach ($is as $key => $value) {
                $res = Db::name('jlscjgykzqk')->where('id',$value['id'])->update($arr[$key]);
                if(!$res){
                    return $this->fail('保存失败!');
                }
            }
        }else{
            $res = Db::name('jlscjgykzqk')->insertAll($arr);
        }

        if($res){
            return $this->success('保存成功', [], 1, 1);
        }
        return $this->fail('保存失败');
    }

    /**
     * @notes  焦炉生产及工艺控制情况上报
     * @return \think\response\Json
     * @author zw
     * @date 2023/1/17 21:59
     */
    public function jlscjgykzqkSubmit()
    {
        $data = input('param.');
        $date = !empty($data['date'])?$data['date']:date('Y-m-d',strtotime('-1 day'));
        if(in_array($data['label'],['生产调度','中润管理员','系统管理员'])){
            $isarr = Db::name('jlscjgykzqk')->where('date',$date)->select();
            if(!empty($isarr)){
                $is['status'] = 0;
            }
        }else{
            $is = Db::name('jlscjgykzqk')->where('date',$date)->find();
        }
        if(!empty($is)){
            // 未提交或审核驳回，就可以上报
            if($is['status']==0||$is['status']==3){
                Db::name('jlscjgykzqk')->where('date',$date)->update([
                    'status'=>1
                ]);
                return $this->success('上报成功', [], 1, 1);
            }elseif($is['status']==1){
                return $this->fail('数据已上报，不能重复上报');
            }elseif($is['status']==2){
                return $this->fail('数据已审核，无法上报');
            }
        }else{
            return $this->fail('请先保存数据，保存后才可上报');
        }
    }

    /**
     * @notes  干熄炉生产及工艺控制情况 获取
     * @return \think\response\Json
     * @author zw
     * @date 2023/1/28 23:59
     */
    public function getGxlscjgykzqk()
    {
        // 接受所有参数
        $data = input('param.');
        $date = !empty($data['date'])?$data['date']:date('Y-m-d',strtotime('-1 day'));
        $is = Db::name('gxlscjgykzqk')->where('date',$date)->select()->toArray();
        if(count($is)==0&&empty($data['isup'])){
            // 获取前一天数据
            $yestoday = date("Y-m-d",(strtotime($date)-86400));
            $is = Db::name('gxlscjgykzqk')->where('date',$yestoday)->select()->toArray();
            foreach ($is as &$value) {
                $value['date'] = $date;
                $value['update_time'] = date('Y-m-d H:i:s');
                unset($value['id']);
                unset($value['aid']);
                unset($value['aname']);
                unset($value['status']);
                unset($value['cid']);
                unset($value['cname']);
                unset($value['check_time']);
            }
            Db::name('gxlscjgykzqk')->insertAll($is);
        }
        return $this->data($is);
    }

    /**
     * @notes  干熄炉生产及工艺控制情况保存
     * @return \think\response\Json
     * @author zw
     * @date 2023/1/28 21:59
     */
    public function gxlscjgykzqkSave()
    {
        // 接受所有参数
        $data = input('param.');
        // 拿到循环插入的数据
        $inputs = $data['inputs'];
        $arr = [];
        $date = !empty($data['date'])?$data['date']:date('Y-m-d',strtotime('-1 day'));
        // 处理数据  匹配字段 并且添加当日时间
        foreach ($inputs as $key => $value) {
            $arr[$key]['aid'] = $data['aid'];
            $arr[$key]['aname'] = $data['aname'];
            foreach ($value as $k => $v) {
                $kk = rtrim($k,$key+1);
                $arr[$key][$kk] = $v;
            }
            $arr[$key]['onefdl'] = $data['onefdl'];
            $arr[$key]['twofdl'] = $data['twofdl'];
            $arr[$key]['yljfdl'] = $data['yljfdl'];
            $arr[$key]['wsrwsl'] = $data['wsrwsl'];
            $arr[$key]['wsyljl'] = $data['wsyljl'];
            $arr[$key]['jiaofen'] = $data['jiaofen'];
            $arr[$key]['jiaomo'] = $data['jiaomo'];
            $arr[$key]['pjzjl'] = $data['pjzjl'];
            $arr[$key]['shaijl'] = $data['shaijl'];
            $arr[$key]['zhongjc'] = $data['zhongjc'];
            $arr[$key]['ganxl'] = $data['ganxl'];
            $arr[$key]['dajc'] = $data['dajc'];
            $arr[$key]['huoyc'] = $data['huoyc'];
            $arr[$key]['content'] = $data['content'];
            $arr[$key]['date'] = $date;
            $arr[$key]['update_time'] = date('Y-m-d H:i:s');
        }
        $is = Db::name('gxlscjgykzqk')->where('date',$date)->select();
        // 如果已经存在当日数据  就更新  否则就新增
        if(count($is)>0){
            $urole = Db::name('admin_role')->where('admin_id',$data['aid'])->find();
            if($data['aid']!=1){
                if($is[0]['status']==1&&!in_array($urole['role_id'],[3,4])){
                    return $this->fail('数据已经上报，不能在进行修改！如有问题请联系生产调度');
                }
                if($is[0]['status']==2&&!in_array($urole['role_id'],[3,4])){
                    return $this->fail('数据已经审核，不能在进行修改！如有问题请联系生产调度');
                }
            }
            foreach ($is as $key => $value) {
                $res = Db::name('gxlscjgykzqk')->where('id',$value['id'])->update($arr[$key]);
                if(!$res){
                    return $this->fail('保存失败!');
                }
            }
        }else{
            $res = Db::name('gxlscjgykzqk')->insertAll($arr);
        }

        if($res){
            return $this->success('保存成功', [], 1, 1);
        }
        return $this->fail('保存失败');
    }

    /**
     * @notes  干熄炉生产及工艺控制情况上报
     * @return \think\response\Json
     * @author zw
     * @date 2023/1/17 21:59
     */
    public function gxlscjgykzqkSubmit()
    {
        $data = input('param.');
        $date = !empty($data['date'])?$data['date']:date('Y-m-d',strtotime('-1 day'));
        if(in_array($data['label'],['生产调度','中润管理员','系统管理员'])){
            $isarr = Db::name('gxlscjgykzqk')->where('date',$date)->select();
            if(!empty($isarr)){
                $is['status'] = 0;
            }
        }else{
            $is = Db::name('gxlscjgykzqk')->where('date',$date)->find();
        }
        if(!empty($is)){
            // 未提交或审核驳回，就可以上报
            if($is['status']==0||$is['status']==3){
                Db::name('gxlscjgykzqk')->where('date',$date)->update([
                    'status'=>1
                ]);
                return $this->success('上报成功', [], 1, 1);
            }elseif($is['status']==1){
                return $this->fail('数据已上报，不能重复上报');
            }elseif($is['status']==2){
                return $this->fail('数据已审核，无法上报');
            }
        }else{
            return $this->fail('请先保存数据，保存后才可上报');
        }
    }
    
    /**
     * @notes  净化生产及工艺控制情况 获取
     * @return \think\response\Json
     * @author zw
     * @date 2023/1/28 23:59
     */
    public function getJhscjgykzqk()
    {
        // 接受所有参数
        $data = input('param.');
        $date = !empty($data['date'])?$data['date']:date('Y-m-d',strtotime('-1 day'));
        $is = Db::name('jhscjgykzqk')->where('date',$date)->find();
        if(empty($is)&&empty($data['isup'])){
            // 获取前一天数据
            $yestoday = date("Y-m-d",(strtotime($date)-86400));
            $is = Db::name('jhscjgykzqk')->where('date',$yestoday)->find();
            if(!empty($is)){
                $is['date'] = $date;
                $is['update_time'] = date('Y-m-d H:i:s');
                unset($is['id']);
                unset($is['aid']);
                unset($is['aname']);
                unset($is['status']);
                unset($is['cid']);
                unset($is['cname']);
                unset($is['check_time']);
                Db::name('jhscjgykzqk')->insert($is);
            }
        }
        return $this->data($is?:[]);
    }

    /**
     * @notes  净化生产及工艺控制情况保存
     * @return \think\response\Json
     * @author zw
     * @date 2023/1/28 21:59
     */
    public function jhscjgykzqkSave()
    {
        // 接受所有参数
        $data = input('param.');
        // 拿到循环插入的数据
        $inputs = $data['inputs'];
        $date = !empty($data['date'])?$data['date']:date('Y-m-d',strtotime('-1 day'));
        $inputs['aid'] = $data['aid'];
        $inputs['aname'] = $data['aname'];
        $inputs['date'] = $date;
        $inputs['update_time'] = date('Y-m-d H:i:s');
        
        $is = Db::name('jhscjgykzqk')->where('date',$date)->find();
        // 如果已经存在当日数据  就更新  否则就新增
        if(!empty($is)){
            $urole = Db::name('admin_role')->where('admin_id',$data['aid'])->find();
            if($data['aid']!=1){
                if($is['status']==1&&!in_array($urole['role_id'],[3,4])){
                    return $this->fail('数据已经上报，不能在进行修改！如有问题请联系生产调度');
                }
                if($is['status']==2&&!in_array($urole['role_id'],[3,4])){
                    return $this->fail('数据已经审核，不能在进行修改！如有问题请联系生产调度');
                }
            }
            $res = Db::name('jhscjgykzqk')->where('id',$is['id'])->update($inputs);
            if(!$res){
                return $this->fail('保存失败!');
            }
        }else{
            $res = Db::name('jhscjgykzqk')->insert($inputs);
        }

        if($res){
            return $this->success('保存成功', [], 1, 1);
        }
        return $this->fail('保存失败');
    }

    /**
     * @notes  净化生产及工艺控制情况上报
     * @return \think\response\Json
     * @author zw
     * @date 2023/1/17 21:59
     */
    public function jhscjgykzqkSubmit()
    {
        $data = input('param.');
        $date = !empty($data['date'])?$data['date']:date('Y-m-d',strtotime('-1 day'));
        if(in_array($data['label'],['生产调度','中润管理员','系统管理员'])){
            $isarr = Db::name('jhscjgykzqk')->where('date',$date)->select();
            if(!empty($isarr)){
                $is['status'] = 0;
            }
        }else{
            $is = Db::name('jhscjgykzqk')->where('date',$date)->find();
        }
        if(!empty($is)){
            // 未提交或审核驳回，就可以上报
            if($is['status']==0||$is['status']==3){
                Db::name('jhscjgykzqk')->where('date',$date)->update([
                    'status'=>1
                ]);
                return $this->success('上报成功', [], 1, 1);
            }elseif($is['status']==1){
                return $this->fail('数据已上报，不能重复上报');
            }elseif($is['status']==2){
                return $this->fail('数据已审核，无法上报');
            }
        }else{
            return $this->fail('请先保存数据，保存后才可上报');
        }
    }

    /**
     * @notes  动力生产情况 获取
     * @return \think\response\Json
     * @author zw
     * @date 2023/1/28 23:59
     */
    public function getDlscqk()
    {
        // 接受所有参数
        $data = input('param.');
        $date = !empty($data['date'])?$data['date']:date('Y-m-d',strtotime('-1 day'));
        $is = Db::name('dlscqk')->where('date',$date)->find();
        if(empty($is)&&empty($data['isup'])){
            // 获取前一天数据
            $yestoday = date("Y-m-d",(strtotime($date)-86400));
            $is = Db::name('dlscqk')->where('date',$yestoday)->find();
            if(!empty($is)){
                $is['date'] = $date;
                $is['update_time'] = date('Y-m-d H:i:s');
                unset($is['id']);
                unset($is['aid']);
                unset($is['aname']);
                unset($is['status']);
                unset($is['cid']);
                unset($is['cname']);
                unset($is['check_time']);
                Db::name('dlscqk')->insert($is);
            }
        }
        return $this->data($is?:[]);
    }

    /**
     * @notes  动力生产情况保存
     * @return \think\response\Json
     * @author zw
     * @date 2023/1/28 21:59
     */
    public function dlscqkSave()
    {
        // 接受所有参数
        $data = input('param.');
        // 拿到循环插入的数据
        $inputs = $data['inputs'];
        $date = !empty($data['date'])?$data['date']:date('Y-m-d',strtotime('-1 day'));
        $inputs['aid'] = $data['aid'];
        $inputs['aname'] = $data['aname'];
        $inputs['date'] = $date;
        $inputs['update_time'] = date('Y-m-d H:i:s');
        
        $is = Db::name('dlscqk')->where('date',$date)->find();
        // 如果已经存在当日数据  就更新  否则就新增
        if(!empty($is)){
            $urole = Db::name('admin_role')->where('admin_id',$data['aid'])->find();
            if($data['aid']!=1){
                if($is['status']==1&&!in_array($urole['role_id'],[3,4])){
                    return $this->fail('数据已经上报，不能在进行修改！如有问题请联系生产调度');
                }
                if($is['status']==2&&!in_array($urole['role_id'],[3,4])){
                    return $this->fail('数据已经审核，不能在进行修改！如有问题请联系生产调度');
                }
            }
            $res = Db::name('dlscqk')->where('id',$is['id'])->update($inputs);
            if(!$res){
                return $this->fail('保存失败!');
            }
        }else{
            $res = Db::name('dlscqk')->insert($inputs);
        }

        if($res){
            return $this->success('保存成功', [], 1, 1);
        }
        return $this->fail('保存失败');
    }

    /**
     * @notes  动力生产情况上报
     * @return \think\response\Json
     * @author zw
     * @date 2023/1/17 21:59
     */
    public function dlscqkSubmit()
    {
        $data = input('param.');
        $date = !empty($data['date'])?$data['date']:date('Y-m-d',strtotime('-1 day'));
        if(in_array($data['label'],['生产调度','中润管理员','系统管理员'])){
            $isarr = Db::name('dlscqk')->where('date',$date)->select();
            if(!empty($isarr)){
                $is['status'] = 0;
            }
        }else{
            $is = Db::name('dlscqk')->where('date',$date)->find();
        }
        if(!empty($is)){
            // 未提交或审核驳回，就可以上报
            if($is['status']==0||$is['status']==3){
                Db::name('dlscqk')->where('date',$date)->update([
                    'status'=>1
                ]);
                return $this->success('上报成功', [], 1, 1);
            }elseif($is['status']==1){
                return $this->fail('数据已上报，不能重复上报');
            }elseif($is['status']==2){
                return $this->fail('数据已审核，无法上报');
            }
        }else{
            return $this->fail('请先保存数据，保存后才可上报');
        }
    }

    /**
     * @notes  甲醇生产情况 获取
     * @return \think\response\Json
     * @author zw
     * @date 2023/1/28 23:59
     */
    public function getJcscqk()
    {
        // 接受所有参数
        $data = input('param.');
        $date = !empty($data['date'])?$data['date']:date('Y-m-d',strtotime('-1 day'));
        $is = Db::name('jcscqk')->where('date',$date)->find();
        if(empty($is)&&empty($data['isup'])){
            // 获取前一天数据
            $yestoday = date("Y-m-d",(strtotime($date)-86400));
            $is = Db::name('jcscqk')->where('date',$yestoday)->find();
            if(!empty($is)){
                $is['date'] = $date;
                $is['update_time'] = date('Y-m-d H:i:s');
                unset($is['id']);
                unset($is['aid']);
                unset($is['aname']);
                unset($is['status']);
                unset($is['cid']);
                unset($is['cname']);
                unset($is['check_time']);
                Db::name('jcscqk')->insert($is);
            }
        }
        return $this->data($is?:[]);
    }

    /**
     * @notes  甲醇生产情况保存
     * @return \think\response\Json
     * @author zw
     * @date 2023/1/28 21:59
     */
    public function jcscqkSave()
    {
        // 接受所有参数
        $data = input('param.');
        // 拿到循环插入的数据
        $inputs = $data['inputs'];
        $date = !empty($data['date'])?$data['date']:date('Y-m-d',strtotime('-1 day'));
        $inputs['aid'] = $data['aid'];
        $inputs['aname'] = $data['aname'];
        $inputs['date'] = $date;
        $inputs['update_time'] = date('Y-m-d H:i:s');
        
        $is = Db::name('jcscqk')->where('date',$date)->find();
        // 如果已经存在当日数据  就更新  否则就新增
        if(!empty($is)){
            $urole = Db::name('admin_role')->where('admin_id',$data['aid'])->find();
            if($data['aid']!=1){
                if($is['status']==1&&!in_array($urole['role_id'],[3,4])){
                    return $this->fail('数据已经上报，不能在进行修改！如有问题请联系生产调度');
                }
                if($is['status']==2&&!in_array($urole['role_id'],[3,4])){
                    return $this->fail('数据已经审核，不能在进行修改！如有问题请联系生产调度');
                }
            }
            $res = Db::name('jcscqk')->where('id',$is['id'])->update($inputs);
            if(!$res){
                return $this->fail('保存失败!');
            }
        }else{
            $res = Db::name('jcscqk')->insert($inputs);
        }

        if($res){
            return $this->success('保存成功', [], 1, 1);
        }
        return $this->fail('保存失败');
    }

    /**
     * @notes  甲醇生产情况上报
     * @return \think\response\Json
     * @author zw
     * @date 2023/1/17 21:59
     */
    public function jcscqkSubmit()
    {
        $data = input('param.');
        $date = !empty($data['date'])?$data['date']:date('Y-m-d',strtotime('-1 day'));
        if(in_array($data['label'],['生产调度','中润管理员','系统管理员'])){
            $isarr = Db::name('jcscqk')->where('date',$date)->select();
            if(!empty($isarr)){
                $is['status'] = 0;
            }
        }else{
            $is = Db::name('jcscqk')->where('date',$date)->find();
        }
        if(!empty($is)){
            // 未提交或审核驳回，就可以上报
            if($is['status']==0||$is['status']==3){
                Db::name('jcscqk')->where('date',$date)->update([
                    'status'=>1
                ]);
                return $this->success('上报成功', [], 1, 1);
            }elseif($is['status']==1){
                return $this->fail('数据已上报，不能重复上报');
            }elseif($is['status']==2){
                return $this->fail('数据已审核，无法上报');
            }
        }else{
            return $this->fail('请先保存数据，保存后才可上报');
        }
    }
    
    /**
     * @notes  精苯生产情况 获取
     * @return \think\response\Json
     * @author zw
     * @date 2023/1/28 23:59
     */
    public function getJbscqk()
    {
        // 接受所有参数
        $data = input('param.');
        $date = !empty($data['date'])?$data['date']:date('Y-m-d',strtotime('-1 day'));
        $is = Db::name('jbscqk')->where('date',$date)->find();
        if(empty($is)&&empty($data['isup'])){
            // 获取前一天数据
            $yestoday = date("Y-m-d",(strtotime($date)-86400));
            $is = Db::name('jbscqk')->where('date',$yestoday)->find();
            if(!empty($is)){
                $is['date'] = $date;
                $is['update_time'] = date('Y-m-d H:i:s');
                unset($is['id']);
                unset($is['aid']);
                unset($is['aname']);
                unset($is['status']);
                unset($is['cid']);
                unset($is['cname']);
                unset($is['check_time']);
                Db::name('jbscqk')->insert($is);
            }
        }
        return $this->data($is?:[]);
    }

    /**
     * @notes  精苯生产情况保存
     * @return \think\response\Json
     * @author zw
     * @date 2023/1/28 21:59
     */
    public function jbscqkSave()
    {
        // 接受所有参数
        $data = input('param.');
        // 拿到循环插入的数据
        $inputs = $data['inputs'];
        $date = !empty($data['date'])?$data['date']:date('Y-m-d',strtotime('-1 day'));
        $inputs['aid'] = $data['aid'];
        $inputs['aname'] = $data['aname'];
        $inputs['date'] = $date;
        $inputs['update_time'] = date('Y-m-d H:i:s');
        
        $is = Db::name('jbscqk')->where('date',$date)->find();
        // 如果已经存在当日数据  就更新  否则就新增
        if(!empty($is)){
            $urole = Db::name('admin_role')->where('admin_id',$data['aid'])->find();
            if($data['aid']!=1){
                if($is['status']==1&&!in_array($urole['role_id'],[3,4])){
                    return $this->fail('数据已经上报，不能在进行修改！如有问题请联系生产调度');
                }
                if($is['status']==2&&!in_array($urole['role_id'],[3,4])){
                    return $this->fail('数据已经审核，不能在进行修改！如有问题请联系生产调度');
                }
            }
            $res = Db::name('jbscqk')->where('id',$is['id'])->update($inputs);
            if(!$res){
                return $this->fail('保存失败!');
            }
        }else{
            $res = Db::name('jbscqk')->insert($inputs);
        }

        if($res){
            return $this->success('保存成功', [], 1, 1);
        }
        return $this->fail('保存失败');
    }
    
    /**
     * @notes  精苯生产情况上报
     * @return \think\response\Json
     * @author zw
     * @date 2023/1/17 21:59
     */
    public function jbscqkSubmit()
    {
        $data = input('param.');
        $date = !empty($data['date'])?$data['date']:date('Y-m-d',strtotime('-1 day'));
        if(in_array($data['label'],['生产调度','中润管理员','系统管理员'])){
            $isarr = Db::name('jbscqk')->where('date',$date)->select();
            if(!empty($isarr)){
                $is['status'] = 0;
            }
        }else{
            $is = Db::name('jbscqk')->where('date',$date)->find();
        }
        if(!empty($is)){
            // 未提交或审核驳回，就可以上报
            if($is['status']==0||$is['status']==3){
                Db::name('jbscqk')->where('date',$date)->update([
                    'status'=>1
                ]);
                return $this->success('上报成功', [], 1, 1);
            }elseif($is['status']==1){
                return $this->fail('数据已上报，不能重复上报');
            }elseif($is['status']==2){
                return $this->fail('数据已审核，无法上报');
            }
        }else{
            return $this->fail('请先保存数据，保存后才可上报');
        }
    }

    /**
     * @notes  园区介质互通情况 获取
     * @return \think\response\Json
     * @author zw
     * @date 2023/1/28 23:59
     */
    public function getYqjzhtqk()
    {
        // 接受所有参数
        $data = input('param.');
        $date = !empty($data['date'])?$data['date']:date('Y-m-d',strtotime('-1 day'));
        $is = Db::name('yqjzhtqk')->where('date',$date)->find();
        if(empty($is)&&empty($data['isup'])){
            // 获取前一天数据
            $yestoday = date("Y-m-d",(strtotime($date)-86400));
            $is = Db::name('yqjzhtqk')->where('date',$yestoday)->find();
            if(!empty($is)){
                $is['date'] = $date;
                $is['update_time'] = date('Y-m-d H:i:s');
                unset($is['id']);
                unset($is['aid']);
                unset($is['aname']);
                unset($is['status']);
                unset($is['cid']);
                unset($is['cname']);
                unset($is['check_time']);
                Db::name('yqjzhtqk')->insert($is);
            }
        }
        return $this->data($is?:[]);
    }

    /**
     * @notes  园区介质互通情况保存
     * @return \think\response\Json
     * @author zw
     * @date 2023/1/28 21:59
     */
    public function yqjzhtqkSave()
    {
        // 接受所有参数
        $data = input('param.');
        // 拿到循环插入的数据
        $inputs = $data['inputs'];
        $date = !empty($data['date'])?$data['date']:date('Y-m-d',strtotime('-1 day'));
        $inputs['aid'] = $data['aid'];
        $inputs['aname'] = $data['aname'];
        $inputs['date'] = $date;
        $inputs['update_time'] = date('Y-m-d H:i:s');
        
        $is = Db::name('yqjzhtqk')->where('date',$date)->find();
        // 如果已经存在当日数据  就更新  否则就新增
        if(!empty($is)){
            $urole = Db::name('admin_role')->where('admin_id',$data['aid'])->find();
            if($data['aid']!=1){
                if($is['status']==1&&!in_array($urole['role_id'],[3,4])){
                    return $this->fail('数据已经上报，不能在进行修改！如有问题请联系生产调度');
                }
                if($is['status']==2&&!in_array($urole['role_id'],[3,4])){
                    return $this->fail('数据已经审核，不能在进行修改！如有问题请联系生产调度');
                }
            }
            $res = Db::name('yqjzhtqk')->where('id',$is['id'])->update($inputs);
            if(!$res){
                return $this->fail('保存失败!');
            }
        }else{
            $res = Db::name('yqjzhtqk')->insert($inputs);
        }

        if($res){
            return $this->success('保存成功', [], 1, 1);
        }
        return $this->fail('保存失败');
    }
    
    /**
     * @notes  园区介质互通情况上报
     * @return \think\response\Json
     * @author zw
     * @date 2023/1/17 21:59
     */
    public function yqjzhtqkSubmit()
    {
        $data = input('param.');
        $date = !empty($data['date'])?$data['date']:date('Y-m-d',strtotime('-1 day'));
        if(in_array($data['label'],['生产调度','中润管理员','系统管理员'])){
            $isarr = Db::name('yqjzhtqk')->where('date',$date)->select();
            if(!empty($isarr)){
                $is['status'] = 0;
            }
        }else{
            $is = Db::name('yqjzhtqk')->where('date',$date)->find();
        }
        if(!empty($is)){
            // 未提交或审核驳回，就可以上报
            if($is['status']==0||$is['status']==3){
                Db::name('yqjzhtqk')->where('date',$date)->update([
                    'status'=>1
                ]);
                return $this->success('上报成功', [], 1, 1);
            }elseif($is['status']==1){
                return $this->fail('数据已上报，不能重复上报');
            }elseif($is['status']==2){
                return $this->fail('数据已审核，无法上报');
            }
        }else{
            return $this->fail('请先保存数据，保存后才可上报');
        }
    }
    
    /**
     * @notes  水质指标分析 获取
     * @return \think\response\Json
     * @author zw
     * @date 2023/1/28 23:59
     */
    public function getHbzb()
    {
        // 接受所有参数
        $data = input('param.');
        $date = !empty($data['date'])?$data['date']:date('Y-m-d',strtotime('-1 day'));
        $is = Db::name('hbzb')->where('date',$date)->find();
        if(empty($is)&&empty($data['isup'])){
            // 获取前一天数据
            $yestoday = date("Y-m-d",(strtotime($date)-86400));
            $is = Db::name('hbzb')->where('date',$yestoday)->find();
            if(!empty($is)){
                $is['date'] = $date;
                $is['update_time'] = date('Y-m-d H:i:s');
                unset($is['id']);
                unset($is['aid']);
                unset($is['aname']);
                unset($is['status']);
                unset($is['cid']);
                unset($is['cname']);
                unset($is['check_time']);
                Db::name('hbzb')->insert($is);
            }
        }
        return $this->data($is?:[]);
    }

    /**
     * @notes  水质指标分析保存
     * @return \think\response\Json
     * @author zw
     * @date 2023/1/28 21:59
     */
    public function hbzbSave()
    {
        // 接受所有参数
        $data = input('param.');
        // 拿到循环插入的数据
        $inputs = $data['inputs'];
        $date = !empty($data['date'])?$data['date']:date('Y-m-d',strtotime('-1 day'));
        $inputs['aid'] = $data['aid'];
        $inputs['aname'] = $data['aname'];
        $inputs['date'] = $date;
        $inputs['update_time'] = date('Y-m-d H:i:s');
        
        $is = Db::name('hbzb')->where('date',$date)->find();
        // 如果已经存在当日数据  就更新  否则就新增
        if(!empty($is)){
            $urole = Db::name('admin_role')->where('admin_id',$data['aid'])->find();
            if($data['aid']!=1){
                if($is['isdl']==1&&$is['isjh']==1){
                    if($is['status']==1&&!in_array($urole['role_id'],[3,4])){
                        return $this->fail('数据已经上报，不能在进行修改！如有问题请联系生产调度');
                    }
                }
                if($is['status']==2&&!in_array($urole['role_id'],[3,4])){
                    return $this->fail('数据已经审核，不能在进行修改！如有问题请联系生产调度');
                }
            }
            $res = Db::name('hbzb')->where('id',$is['id'])->update($inputs);
            if(!$res){
                return $this->fail('保存失败!');
            }
        }else{
            $res = Db::name('hbzb')->insert($inputs);
        }

        if($res){
            return $this->success('保存成功', [], 1, 1);
        }
        return $this->fail('保存失败');
    }
    
    /**
     * @notes  水质指标分析上报
     * @return \think\response\Json
     * @author zw
     * @date 2023/1/17 21:59
     */
    public function hbzbSubmit()
    {
        $data = input('param.');
        $date = !empty($data['date'])?$data['date']:date('Y-m-d',strtotime('-1 day'));
        $up = [
            "status"=>1
        ];
        if(in_array($data['label'],['生产调度','中润管理员','系统管理员'])){
            $isarr = Db::name('hbzb')->where('date',$date)->select();
            if(!empty($isarr)){
                $is['status'] = 0;
            }
        }else{
            if($data['label']=='动力分厂'){//动力分厂
                $up['isdl'] = 1;
            }elseif($data['label']=='净化分厂'){//净化分厂
                $up['isjh'] = 1;
            }else{//管理员
                $up['isdl'] = 1;
                $up['isjh'] = 1;
            }
            $is = Db::name('hbzb')->where('date',$date)->find();
        }
        if(!empty($is)){
            // 未提交或审核驳回，就可以上报
            if($is['status']==0||$is['status']==3||$is['isdl']==0||$is['isjh']==0){
                Db::name('hbzb')->where('date',$date)->update($up);
                return $this->success('上报成功', [], 1, 1);
            }elseif($is['status']==1){
                return $this->fail('数据已上报，不能重复上报');
            }elseif($is['status']==2){
                return $this->fail('数据已审核，无法上报');
            }
        }else{
            return $this->fail('请先保存数据，保存后才可上报');
        }
    }

    /**
     * @notes  日生产分析 获取
     * @return \think\response\Json
     * @author zw
     * @date 2023/1/28 23:59
     */
    public function getRscfx()
    {
        // 接受所有参数
        $data = input('param.');
        $newdate = date('Y-m-d',strtotime('-1 day'));
        // 如果大于17点 那么就按照今天的数据进行存储
        if(date('H')>=17){
            $newdate = date('Y-m-d');
        }
        $date = !empty($data['date'])?$data['date']:$newdate;
        if(in_array($data['label'],['生产调度','中润管理员','系统管理员'])){
            $is = Db::name('rscfx')->where('date',$date)->select()->toArray();
        }else{
            $is = Db::name('rscfx')->where('date',$date)->where('label',$data['label'])->select()->toArray();
        }
        // var_dump($is);die;  
        if(count($is)==0&&empty($data['isup'])){
            // 获取前一天数据
            $yestoday = date("Y-m-d",(strtotime($date)-86400));
            $is = Db::name('rscfx')->where('date',$yestoday)->select()->toArray();
            foreach ($is as &$value) {
                $value['date'] = $date;
                $value['update_time'] = date('Y-m-d H:i:s');
                unset($value['id']);
                unset($value['aid']);
                unset($value['aname']);
                unset($value['is']);
                unset($value['status']);
                unset($value['cid']);
                unset($value['cname']);
                unset($value['check_time']);
            }
            Db::name('rscfx')->insertAll($is);
        }
        return $this->data($is);
    }

    /**
     * @notes  日生产分析保存
     * @return \think\response\Json
     * @author zw
     * @date 2023/1/28 21:59
     */
    public function rscfxSave()
    {
        // 接受所有参数
        $data = input('param.');
        // 拿到循环插入的数据
        $inputs = $data['inputs'];
        $newdate = date('Y-m-d',strtotime('-1 day'));
        // 如果大于17点 那么就按照今天的数据进行存储
        if(date('H')>=17){
            $newdate = date('Y-m-d');
        }
        $date = !empty($data['date'])?$data['date']:$newdate;

        foreach ($inputs as $key => $value) {
            $key = $value['no'];
            $arr[$key]['aid'] = $data['aid'];
            $arr[$key]['aname'] = $data['aname'];
            $arr[$key]['label'] = $value['label'];
            $arr[$key]['title'] = $value['title'];
            $arr[$key]['content'] = $value['content'];
            $arr[$key]['date'] = $date;
            $arr[$key]['update_time'] = date('Y-m-d H:i:s');
            $arr[$key]['is'] = $value['is'];
            $arr[$key]['no'] = $value['no'];
        }
        // var_dump($arr);die;

        $is = Db::name('rscfx')->where('date',$date)->select();
        if(count($is)>0){
            // 如果能查到分厂 就证明是分厂用户  单独更新。否则就是管理员 直接全部更新
            $isfenchang = Db::name('rscfx')->where('label',$data['label'])->where('date',$date)->find();
            if(!empty($isfenchang)){
                if($isfenchang['status']==1){
                    return $this->fail('数据已经上报，不能在进行修改！如有问题请联系生产调度');
                }
                if($isfenchang['status']==2){
                    return $this->fail('数据已经审核，不能在进行修改！如有问题请联系生产调度');
                }
                $res = Db::name('rscfx')->where('label',$data['label'])->where('date',$date)->update($arr[$isfenchang['no']]);
            }else{
                foreach ($is as $key => $value) {
                    $res = Db::name('rscfx')->where('id',$value['id'])->update($arr[$value['no']]);
                    if(!$res){
                        return $this->fail('保存失败!');
                    }
                }
            }
        }else{
            $res = Db::name('rscfx')->insertAll($arr);
        }

        if($res){
            return $this->success('保存成功', [], 1, 1);
        }
        return $this->fail('保存失败');
    }

    /**
     * @notes  日生产分析上报
     * @return \think\response\Json
     * @author zw
     * @date 2023/1/17 21:59
     */
    public function rscfxSubmit()
    {
        $data = input('param.');
        $newdate = date('Y-m-d',strtotime('-1 day'));
        // 如果大于17点 那么就按照今天的数据进行存储
        if(date('H')>=17){
            $newdate = date('Y-m-d');
        }
        $date = !empty($data['date'])?$data['date']:$newdate;
        if(in_array($data['label'],['生产调度','中润管理员','系统管理员'])){
            $isarr = Db::name('rscfx')->where('date',$date)->select();
            if(!empty($isarr)){
                $is['status'] = 0;
            }
        }else{
            $is = Db::name('rscfx')->where('date',$date)->where('is',1)->where('label',$data['label'])->find();
        }
        
        if(!empty($is)){
            // 未提交或审核驳回，就可以上报
            if($is['status']==0||$is['status']==3){
                if(in_array($data['label'],['生产调度','中润管理员','系统管理员'])){
                    Db::name('rscfx')->where('date',$date)->update([
                        'status'=>1
                    ]);
                }else{
                    Db::name('rscfx')->where('date',$date)->where('is',1)->where('label',$data['label'])->update([
                        'status'=>1
                    ]);
                }
                return $this->success('上报成功', [], 1, 1);
            }elseif($is['status']==1){
                return $this->fail('数据已上报，不能重复上报');
            }elseif($is['status']==2){
                return $this->fail('数据已审核，无法上报');
            }
        }else{
            return $this->fail('请先保存数据，保存后才可上报');
        }
    }
    
    /**
     * @notes  日生产分析 获取
     * @return \think\response\Json
     * @author zw
     * @date 2023/1/28 23:59
     */
    public function getRgcfx()
    {
        // 接受所有参数
        $data = input('param.');
        $newdate = date('Y-m-d',strtotime('-1 day'));
        // 如果大于17点 那么就按照今天的数据进行存储
        if(date('H')>=17){
            $newdate = date('Y-m-d');
        }
        $date = !empty($data['date'])?$data['date']:$newdate;
        if(in_array($data['label'],['生产调度','中润管理员','系统管理员'])){
            $is = Db::name('rgcfx')->where('date',$date)->select()->toArray();
        }else{
            $is = Db::name('rgcfx')->where('date',$date)->where('label',$data['label'])->select()->toArray();
        }
        // var_dump($is);die;  
        if(count($is)==0&&empty($data['isup'])){
            // 获取前一天数据
            $yestoday = date("Y-m-d",(strtotime($date)-86400));
            $is = Db::name('rgcfx')->where('date',$yestoday)->select()->toArray();
            foreach ($is as &$value) {
                $value['date'] = $date;
                $value['update_time'] = date('Y-m-d H:i:s');
                unset($value['id']);
                unset($value['aid']);
                unset($value['aname']);
                unset($value['is']);
                unset($value['status']);
                unset($value['cid']);
                unset($value['cname']);
                unset($value['check_time']);
            }
            Db::name('rgcfx')->insertAll($is);
        }
        return $this->data($is);
    }

    /**
     * @notes  日工程分析保存
     * @return \think\response\Json
     * @author zw
     * @date 2023/1/28 21:59
     */
    public function rgcfxSave()
    {
        // 接受所有参数
        $data = input('param.');
        // 拿到循环插入的数据
        $inputs = $data['inputs'];
        $newdate = date('Y-m-d',strtotime('-1 day'));
        // 如果大于17点 那么就按照今天的数据进行存储
        if(date('H')>=17){
            $newdate = date('Y-m-d');
        }
        $date = !empty($data['date'])?$data['date']:$newdate;
        
        foreach ($inputs as $key => $value) {
            $key = $value['no'];
            $arr[$key]['aid'] = $data['aid'];
            $arr[$key]['aname'] = $data['aname'];
            $arr[$key]['label'] = $value['label'];
            $arr[$key]['content'] = $value['content'];
            $arr[$key]['date'] = $date;
            $arr[$key]['update_time'] = date('Y-m-d H:i:s');
            $arr[$key]['is'] = $value['is'];
            $arr[$key]['no'] = $value['no'];
        }
        // var_dump($arr);die;

        $is = Db::name('rgcfx')->where('date',$date)->select();
        if(count($is)>0){
            // 如果能查到分厂 就证明是分厂用户  单独更新。否则就是管理员 直接全部更新
            $isfenchang = Db::name('rgcfx')->where('label',$data['label'])->where('date',$date)->find();
            if(!empty($isfenchang)){
                if($isfenchang['status']==1){
                    return $this->fail('数据已经上报，不能在进行修改！如有问题请联系生产调度');
                }
                if($isfenchang['status']==2){
                    return $this->fail('数据已经审核，不能在进行修改！如有问题请联系生产调度');
                }
                $res = Db::name('rgcfx')->where('label',$data['label'])->where('date',$date)->update($arr[$isfenchang['no']]);
            }else{
                foreach ($is as $key => $value) {
                    $res = Db::name('rgcfx')->where('id',$value['id'])->update($arr[$value['no']]);
                    if(!$res){
                        return $this->fail('保存失败!');
                    }
                }
            }
        }else{
            $res = Db::name('rgcfx')->insertAll($arr);
        }

        if($res){
            return $this->success('保存成功', [], 1, 1);
        }
        return $this->fail('保存失败');
    }

    /**
     * @notes  日工程分析上报
     * @return \think\response\Json
     * @author zw
     * @date 2023/1/17 21:59
     */
    public function rgcfxSubmit()
    {
        $data = input('param.');
        $newdate = date('Y-m-d',strtotime('-1 day'));
        // 如果大于17点 那么就按照今天的数据进行存储
        if(date('H')>=17){
            $newdate = date('Y-m-d');
        }
        $date = !empty($data['date'])?$data['date']:$newdate;
        if(in_array($data['label'],['生产调度','中润管理员','系统管理员'])){
            $isarr = Db::name('rgcfx')->where('date',$date)->select();
            if(!empty($isarr)){
                $is['status'] = 0;
            }
        }else{
            $is = Db::name('rgcfx')->where('date',$date)->where('is',1)->where('label',$data['label'])->find();
        }
        if(!empty($is)){
            // 未提交或审核驳回，就可以上报
            if($is['status']==0||$is['status']==3){
                if(in_array($data['label'],['生产调度','中润管理员','系统管理员'])){
                    Db::name('rgcfx')->where('date',$date)->update([
                        'status'=>1
                    ]);
                }else{
                    Db::name('rgcfx')->where('date',$date)->where('is',1)->where('label',$data['label'])->update([
                        'status'=>1
                    ]);
                }
                return $this->success('上报成功', [], 1, 1);
            }elseif($is['status']==1){
                return $this->fail('数据已上报，不能重复上报');
            }elseif($is['status']==2){
                return $this->fail('数据已审核，无法上报');
            }
        }else{
            return $this->fail('请先保存数据，保存后才可上报');
        }
    }
    
    /**
     * @notes  各分厂药剂库存 获取
     * @return \think\response\Json
     * @author zw
     * @date 2023/1/28 23:59
     */
    public function getStock()
    {
        // 接受所有参数
        $data = input('param.');
        $date = !empty($data['date'])?$data['date']:date('Y-m-d',strtotime('-1 day'));
        $where = [
            "date"=>$date,
        ];
        if(!empty($data['ishistory'])){
            $where['status'] = 2;
        }
        if(in_array($data['company'],['生产调度','中润管理员','系统管理员'])){
            $is = Db::name('stock')->where($where)->select()->toArray();
        }else{
            $where['company'] = $data['company'];
            $is = Db::name('stock')->where($where)->select()->toArray();
        }
        if(count($is)==0&&empty($data['ishistory'])&&empty($data['isup'])){
            // 获取前一天数据
            $where1['date'] = date("Y-m-d",(strtotime($date)-86400));
            $is = Db::name('stock')->where($where1)->select()->toArray();
            foreach ($is as &$value) {
                $value['date'] = $date;
                $value['update_time'] = date('Y-m-d H:i:s');
                unset($value['id']);
                unset($value['aid']);
                unset($value['aname']);
                unset($value['is']);
                unset($value['status']);
                unset($value['cid']);
                unset($value['cname']);
                unset($value['check_time']);
                $res = Db::name('stock')->where('date',$date)->where('no',$value['no'])->find();
                if(empty($res)){
                    Db::name('stock')->insert($value);
                }
            }
            $is = Db::name('stock')->where($where)->select()->toArray();
        }
        return $this->data($is?:[]);
    }
    
    /**
     * @notes  药剂库存保存
     * @return \think\response\Json
     * @author zw
     * @date 2023/1/28 21:59
     */
    public function stockSave()
    {
        // 接受所有参数
        $data = input('param.');
        if(empty($data['aid'])||empty($data['aname'])){
            return $this->fail('请重新登录后在进行操作!');
        }
        // 拿到循环插入的数据
        $inputs = $data['inputs'];
        $arr = [];
        $date = !empty($data['date'])?$data['date']:date('Y-m-d',strtotime('-1 day'));
        // 处理数据  匹配字段 并且添加当日时间
        foreach ($inputs as $value) {
            $key = $value['no'];
            $arr[$key]['aid'] = $data['aid'];
            $arr[$key]['aname'] = $data['aname'];
            
            $arr[$key]['name'] = $value['name'];
            $arr[$key]['pname'] = $value['pname'];
            $arr[$key]['percentage'] = $value['percentage'];
            $arr[$key]['mode'] = $value['mode'];
            $arr[$key]['limit'] = $value['limit'];
            $arr[$key]['kc'] = $value['kc'];
            $arr[$key]['dryl'] = $value['dryl'];
            $arr[$key]['unit1'] = $value['unit1'];
            $arr[$key]['company'] = $value['company'];
            $arr[$key]['kckyts'] = $value['kckyts'];
            $arr[$key]['bz'] = $value['bz'];
            $arr[$key]['place'] = $value['place'];
            $arr[$key]['effect'] = $value['effect'];
            $arr[$key]['is'] = $value['is'];

            $arr[$key]['date'] = $date;
            $arr[$key]['update_time'] = date('Y-m-d H:i:s');
            $arr[$key]['no'] = $value['no'];
        }

        $is = Db::name('stock')->where('date',$date)->select();
        // 如果已经存在当日数据  就更新  否则就新增
        if(count($is)>0){
            // 如果能查到分厂 就证明是分厂用户  单独更新。否则就是管理员 直接全部更新
            $isfenchang = Db::name('stock')->where('company',$data['company'])->where('date',$date)->select()->toArray();
            // var_dump($arr);die;
            if(!empty($isfenchang)){
                if($isfenchang[0]['status']==1){
                    return $this->fail('数据已经上报，不能在进行修改！如有问题请联系生产调度');
                }
                if($isfenchang[0]['status']==2){
                    return $this->fail('数据已经审核，不能在进行修改！如有问题请联系生产调度');
                }
                foreach ($isfenchang as $key => $value) {
                    $res = Db::name('stock')->where('id',$value['id'])->update($arr[$value['no']]);
                    if(!$res){
                        return $this->fail('保存失败!');
                    }
                }
            }else{
                foreach ($is as $key => $value) {
                    $res = Db::name('stock')->where('id',$value['id'])->update($arr[$value['no']]);
                    if(!$res){
                        return $this->fail('保存失败!');
                    }
                }
            }
        }else{
            $res = Db::name('stock')->insertAll($arr);
        }

        if($res){
            return $this->success('保存成功', [], 1, 1);
        }
        return $this->fail('保存失败');
    }
    
    /**
     * @notes  药剂库存上报
     * @return \think\response\Json
     * @author zw
     * @date 2023/1/17 21:59
     */
    public function stockSubmit()
    {
        $data = input('param.');
        $date = !empty($data['date'])?$data['date']:date('Y-m-d',strtotime('-1 day'));
        if(in_array($data['company'],['生产调度','中润管理员','系统管理员'])){
            $isarr = Db::name('stock')->where('date',$date)->select();
            if(!empty($isarr)){
                $is['status'] = 0;
            }
        }else{
            $is = Db::name('stock')->where('date',$date)->where('is',1)->where('company',$data['company'])->find();
        }
        if(!empty($is)){
            // 未提交或审核驳回，就可以上报
            if($is['status']==0||$is['status']==3){
                if(in_array($data['company'],['生产调度','中润管理员','系统管理员'])){
                    Db::name('stock')->where('date',$date)->update([
                        'status'=>1
                    ]);
                }else{
                    Db::name('stock')->where('date',$date)->where('is',1)->where('company',$data['company'])->update([
                        'status'=>1
                    ]);
                }
                return $this->success('上报成功', [], 1, 1);
            }elseif($is['status']==1){
                return $this->fail('数据已上报，不能重复上报');
            }elseif($is['status']==2){
                return $this->fail('数据已审核，无法上报');
            }
        }else{
            return $this->fail('请先保存数据，保存后才可上报');
        }
    }

    // 定时器1  0:00-2:00
    public function crontab(){
        $date = date("Y-m-d",strtotime("-1 day"));
        $where = [
            ['date','=',$date],
            ['status','not in',[2]]
        ];
        // 上报
        $data = [
            "status"=>1
        ];
        try {
            Db::startTrans();
            // 1.备煤分厂  原料煤管理情况 + 配煤指标及焦炭质量情况的一半（另一半质检中心）
            $iscp = Db::name('ylmglqk')->where($where)->find();
            if(!empty($iscp)){
                Db::name('ylmglqk')->where($where)->update($data);
            }
            $iscp = Db::name('pmzbjjtzlqk')->where($where)->find();
            if(!empty($iscp)){
                $data['isbm'] = 1;
                Db::name('pmzbjjtzlqk')->where($where)->update($data);
                unset($data['isbm']);
            }

            // 销售部  产品销售及化工类原料进出厂情况
            $iscp = Db::name('cpxsjhglyljcqk')->where($where)->find();
            if(!empty($iscp)){
                Db::name('cpxsjhglyljcqk')->where($where)->update($data);
            }

            // 2.炼焦分厂 焦炉生产及工艺控制情况
            $iscp = Db::name('jlscjgykzqk')->where($where)->find();
            if(!empty($iscp)){
                Db::name('jlscjgykzqk')->where($where)->update($data);
            }
            
            // 3干熄焦分厂 干熄炉生产及工艺控制情况
            $iscp = Db::name('gxlscjgykzqk')->where($where)->find();
            if(!empty($iscp)){
                Db::name('gxlscjgykzqk')->where($where)->update($data);
            }
 
            // 4净化分厂 净化生产及工艺控制情况
            $iscp = Db::name('jhscjgykzqk')->where($where)->find();
            if(!empty($iscp)){
                Db::name('jhscjgykzqk')->where($where)->update($data);
            }

            // 5动力分厂 动力生产情况
            $iscp = Db::name('dlscqk')->where($where)->find();
            if(!empty($iscp)){
                Db::name('dlscqk')->where($where)->update($data);
            }

            // 6甲醇分厂 甲醇生产情况
            $iscp = Db::name('jcscqk')->where($where)->find();
            if(!empty($iscp)){
                Db::name('jcscqk')->where($where)->update($data);
            }

            // 7精苯分厂 精苯生产情况
            $iscp = Db::name('jbscqk')->where($where)->find();
            if(!empty($iscp)){
                Db::name('jbscqk')->where($where)->update($data);
            }

            // 水质指标分析  动力分厂  +  净化分厂
            $iscp = Db::name('hbzb')->where($where)->find();
            if(!empty($iscp)){
                $data['isdl'] = 1;
                $data['isjh'] = 1;
                Db::name('hbzb')->where($where)->update($data);
                unset($data['isdl']);
                unset($data['isjh']);
            }

            // 库存
            $iscp = Db::name('stock')->where($where)->find();
            if(!empty($iscp)){
                $data['is'] = 1;
                Db::name('stock')->where($where)->update($data);
                unset($data['is']);
            }

            // 提交事务
            Db::commit();
            
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            Log::write($date.'0:00-2:00 各分厂+水质指标分析自动上报定时执行失败，失败原因：'.$e->getMessage(),'error');
        }
    }

    // 定时器2  19:00
    public function crontab2(){
        $date = date("Y-m-d",strtotime("-1 day"));
        $where = [
            ['date','=',$date],
            ['status','not in',[2]]
        ];
        // 上报
        $data = [
            "status"=>1,
            "is"=>1
        ];
        try {
            Db::startTrans();

            // 19点上报当天白班数据 质检中心 配煤指标及焦炭质量情况的另一半（另一半由备煤分厂填报）
            $where1 = [
                ['date','=',date("Y-m-d")],
                ['status','not in',[2]]
            ];
            $iscp = Db::name('pmzbjjtzlqk')->where($where1)->find();
            // echo Db::getLastSql();die;
            if(!empty($iscp)){
                Db::name('pmzbjjtzlqk')->where($where1)->update(['iszj'=>1]);
            }

            // 日工程分析
            $iscp = Db::name('rgcfx')->where($where)->find();
            if(!empty($iscp)){
                Db::name('rgcfx')->where($where)->update($data);
            }

            // 日生产分析
            $iscp = Db::name('rscfx')->where($where)->find();
            if(!empty($iscp)){
                Db::name('rscfx')->where($where)->update($data);
            }

            // 提交事务
            Db::commit();
            
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            Log::record($date.'19:00日工程分析和日生产分析，质检中心白班自动上报定时执行失败，失败原因：'.$e->getMessage(),'error');
        }
    }

    // 定时器3  7:00 上报昨天夜班数据 质检中心
    public function crontab3(){
        $date = date("Y-m-d",strtotime("-1 day"));
        $where = [
            ['date','=',$date],
            ['status','not in',[2]]
        ];
        try {
            Db::startTrans();

            // 7点上报昨天夜班数据 质检中心 配煤指标及焦炭质量情况的另一半（另一半由备煤分厂填报）
            $iscp = Db::name('pmzbjjtzlqk')->where($where)->find();
            if(!empty($iscp)){
                Db::name('pmzbjjtzlqk')->where($where)->update(['iszj'=>1]);
            }

            // 提交事务
            Db::commit();
            
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            Log::record($date.'7:00质检中心夜班自动上报定时执行失败，失败原因：'.$e->getMessage(),'error');
        }
    }

    // 定时器4  16:30 复制生成今日数据
    public function crontab4(){
        $where['date'] = date("Y-m-d",strtotime("-1 day"));
        $date = date("Y-m-d");
        $datetime = date("Y-m-d H:i:s");
        try {
            Db::startTrans();
            // 原料煤管理情况
            $is = Db::name('ylmglqk')->where($where)->select()->toArray();
            foreach ($is as &$value) {
                $value['date'] = $date;
                $value['update_time'] = $datetime;
                unset($value['id']);
                unset($value['aid']);
                unset($value['aname']);
                unset($value['status']);
                unset($value['cid']);
                unset($value['cname']);
                unset($value['check_time']);
            }
            Db::name('ylmglqk')->insertAll($is);
            // 配煤指标及焦炭质量情况
            $is = Db::name('pmzbjjtzlqk')->where($where)->select()->toArray();
            foreach ($is as &$value) {
                $value['date'] = $date;
                $value['update_time'] = $datetime;
                unset($value['id']);
                unset($value['aid']);
                unset($value['aname']);
                unset($value['status']);
                unset($value['cid']);
                unset($value['cname']);
                unset($value['check_time']);
            }
            Db::name('pmzbjjtzlqk')->insertAll($is);
            // 产品销售及化工类原料进出厂情况
            $is = Db::name('cpxsjhglyljcqk')->where($where)->select()->toArray();
            foreach ($is as &$value) {
                $value['date'] = $date;
                $value['update_time'] = $datetime;
                unset($value['id']);
                unset($value['aid']);
                unset($value['aname']);
                unset($value['status']);
                unset($value['cid']);
                unset($value['cname']);
                unset($value['check_time']);
            }
            Db::name('cpxsjhglyljcqk')->insertAll($is);
            // 焦炉生产及工艺控制情况
            $is = Db::name('jlscjgykzqk')->where($where)->select()->toArray();
            foreach ($is as &$value) {
                $value['date'] = $date;
                $value['update_time'] = $datetime;
                unset($value['id']);
                unset($value['aid']);
                unset($value['aname']);
                unset($value['status']);
                unset($value['cid']);
                unset($value['cname']);
                unset($value['check_time']);
            }
            Db::name('jlscjgykzqk')->insertAll($is);
            // 干熄炉生产及工艺控制情况
            $is = Db::name('gxlscjgykzqk')->where($where)->select()->toArray();
            foreach ($is as &$value) {
                $value['date'] = $date;
                $value['update_time'] = $datetime;
                unset($value['id']);
                unset($value['aid']);
                unset($value['aname']);
                unset($value['status']);
                unset($value['cid']);
                unset($value['cname']);
                unset($value['check_time']);
            }
            Db::name('gxlscjgykzqk')->insertAll($is);
            // 净化生产及工艺控制情况
            $is = Db::name('jhscjgykzqk')->where($where)->find();
            if(!empty($is)){
                $is['date'] = $date;
                $is['update_time'] = $datetime;
                unset($is['id']);
                unset($is['aid']);
                unset($is['aname']);
                unset($is['status']);
                unset($is['cid']);
                unset($is['cname']);
                unset($is['check_time']);
                Db::name('jhscjgykzqk')->insert($is);
            }
            // 动力生产情况
            $is = Db::name('dlscqk')->where($where)->find();
            if(!empty($is)){
                $is['date'] = $date;
                $is['update_time'] = $datetime;
                unset($is['id']);
                unset($is['aid']);
                unset($is['aname']);
                unset($is['status']);
                unset($is['cid']);
                unset($is['cname']);
                unset($is['check_time']);
                Db::name('dlscqk')->insert($is);
            }
            // 甲醇生产情况
            $is = Db::name('jcscqk')->where($where)->find();
            if(!empty($is)){
                $is['date'] = $date;
                $is['update_time'] = $datetime;
                unset($is['id']);
                unset($is['aid']);
                unset($is['aname']);
                unset($is['status']);
                unset($is['cid']);
                unset($is['cname']);
                unset($is['check_time']);
                Db::name('jcscqk')->insert($is);
            }
            // 精苯生产情况
            $is = Db::name('jbscqk')->where($where)->find();
            if(!empty($is)){
                $is['date'] = $date;
                $is['update_time'] = $datetime;
                unset($is['id']);
                unset($is['aid']);
                unset($is['aname']);
                unset($is['status']);
                unset($is['cid']);
                unset($is['cname']);
                unset($is['check_time']);
                Db::name('jbscqk')->insert($is);
            }
            // 园区介质互通情况
            $is = Db::name('yqjzhtqk')->where($where)->find();
            if(!empty($is)){
                $is['date'] = $date;
                $is['update_time'] = $datetime;
                unset($is['id']);
                unset($is['aid']);
                unset($is['aname']);
                unset($is['status']);
                unset($is['cid']);
                unset($is['cname']);
                unset($is['check_time']);
                Db::name('yqjzhtqk')->insert($is);
            }
            // 水质指标分析
            $is = Db::name('hbzb')->where($where)->find();
            if(!empty($is)){
                $is['date'] = $date;
                $is['update_time'] = $datetime;
                unset($is['id']);
                unset($is['aid']);
                unset($is['aname']);
                unset($is['status']);
                unset($is['cid']);
                unset($is['cname']);
                unset($is['check_time']);
                Db::name('hbzb')->insert($is);
            }
            // 日生产分析数据
            $is = Db::name('rscfx')->where($where)->select()->toArray();
            foreach ($is as &$value) {
                $value['date'] = $date;
                $value['update_time'] = $datetime;
                unset($value['id']);
                unset($value['aid']);
                unset($value['aname']);
                unset($value['is']);
                unset($value['status']);
                unset($value['cid']);
                unset($value['cname']);
                unset($value['check_time']);
            }
            Db::name('rscfx')->insertAll($is);
            // 日工程分析
            $is = Db::name('rgcfx')->where($where)->select()->toArray();
            foreach ($is as &$value) {
                $value['date'] = $date;
                $value['update_time'] = $datetime;
                unset($value['id']);
                unset($value['aid']);
                unset($value['aname']);
                unset($value['is']);
                unset($value['status']);
                unset($value['cid']);
                unset($value['cname']);
                unset($value['check_time']);
            }
            Db::name('rgcfx')->insertAll($is);
            // 药剂库存数据
            $is = Db::name('stock')->where($where)->select()->toArray();
            foreach ($is as &$value) {
                $value['date'] = $date;
                $value['update_time'] = $datetime;
                unset($value['id']);
                unset($value['aid']);
                unset($value['aname']);
                unset($value['is']);
                unset($value['status']);
                unset($value['cid']);
                unset($value['cname']);
                unset($value['check_time']);
            }
            Db::name('stock')->insertAll($is);
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            Log::record($date.'16:30复制生成数据，失败原因：'.$e->getMessage(),'error');
        }
    }
}